/**
 * 
 */
package com.ieasy.module.system.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.exception.ServiceException;
import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.model.Pager;
import com.ieasy.basic.model.SystemContext;
import com.ieasy.basic.util.BeanUtils;
import com.ieasy.basic.util.StringUtil;
import com.ieasy.module.common.web.servlet.WebContextUtil;
import com.ieasy.module.system.entity.DeptInfoCategoryEntity;
import com.ieasy.module.system.entity.OrgEntity;
import com.ieasy.module.system.web.form.DeptInfoCategoryForm;

/**
 * @author 杨浩泉
 *
 */
@Service @Transactional
public class DeptInfoCategoryService implements IDeptInfoCategoryService {
	
	@Inject
	private IBaseDao<DeptInfoCategoryEntity> basedaoDeptInfoCategory ;
	
	@Override
	public Msg add(DeptInfoCategoryForm form) {
		DeptInfoCategoryEntity entity = new DeptInfoCategoryEntity() ;
		BeanUtils.copyNotNullProperties(form, entity, new String[]{"deptid"}) ;
		OrgEntity o = new OrgEntity();
		o.setId(form.getDeptid());
		entity.setDeptid(o);
		entity.setCreateName(WebContextUtil.getCurrentUser().getUser().getEmp_name()) ;
		entity.setCreated(new Date()) ;
		
		this.basedaoDeptInfoCategory.add(entity) ;
		return new Msg(true, "添加成功！");
	}

	@Override
	public Msg delete(DeptInfoCategoryForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					this.basedaoDeptInfoCategory.delete(DeptInfoCategoryEntity.class, id) ;
				}
				return new Msg(true, "删除成功！") ;
			}
		} catch (Exception e) {
			return new Msg(false, "删除失败！") ;
		}
		return null;
	}
	
	@Override
	public Msg update(DeptInfoCategoryForm form) {
		DeptInfoCategoryEntity entity = this.basedaoDeptInfoCategory.load(DeptInfoCategoryEntity.class, form.getId()) ;
		BeanUtils.copyNotNullProperties(form, entity, new String[]{"created", "createName", "deptid"}) ;
		entity.setModifyName(WebContextUtil.getCurrentUser().getUser().getEmp_name()) ;
		entity.setModifyDate(new Date()) ;
		
		OrgEntity o = new OrgEntity();
		o.setId(form.getDeptid());
		entity.setDeptid(o);
		
		this.basedaoDeptInfoCategory.update(entity) ;
		
		return new Msg(true, "修改成功！");
	}
	
	@Override
	public DeptInfoCategoryForm get(DeptInfoCategoryForm form) {
		DeptInfoCategoryEntity entity = this.basedaoDeptInfoCategory.load(DeptInfoCategoryEntity.class, form.getId()) ;
		BeanUtils.copyNotNullProperties(entity, form, new String[]{"deptid"}) ;
		form.setDeptid(entity.getDeptid().getId());
		return form ;
	}

	@Override
	public DataGrid datagrid(DeptInfoCategoryForm form) {
		
		if(null != form.getSort()) {
			String[] sort = new String[]{"px"};
			if(StringUtil.arrayToString(sort).contains(form.getSort())) {
				SystemContext.setSort("t."+form.getSort()) ; SystemContext.setOrder(form.getOrder()) ;
			}
		}
		try {
			Pager<DeptInfoCategoryForm> pager = this.find(form) ;
			DataGrid dg = new DataGrid() ;
			dg.setTotal(pager.getTotal());
			dg.setRows(pager.getDataRows());
			return dg;
		} catch (Exception e) {
			throw new ServiceException("加载信息异常", e) ;
		}
	}


	private Pager<DeptInfoCategoryForm> find(DeptInfoCategoryForm form) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String sql = "select " +
					 "t.id, t.name, " +
					 "t.created, t.createName, t.modifyName, t.modifyDate, " +
					 "o.name as deptName " +
				 	 "from ieasy_sys_deptinfo_category t " +
				 	 "join ieasy_sys_org o on(o.id=t.deptid) " +
				 	 "where 1=1 ";
		sql = addWhere(sql, form, alias) ;
		return this.basedaoDeptInfoCategory.findSQL(sql, alias, DeptInfoCategoryForm.class, false) ;
	}

	private String addWhere(String hql, DeptInfoCategoryForm form, Map<String, Object> params) {
		if (form.getName() != null && !"".equals(form.getName())) {
			hql += " and t.name=:name";
			params.put("name", form.getName());
		}
		if (form.getDeptid() != null && !"".equals(form.getDeptid())) {
			hql += " and o.id=:deptid";
			params.put("deptid", form.getDeptid());
		}
		if (form.getCreateName() != null && !"".equals(form.getCreateName())) {
			hql += " and t.createName=:createName";
			params.put("createName", form.getCreateName());
		}
		return hql;
	}
	
	@Override
	public List<DeptInfoCategoryForm> getCategoryList() {
		String sql = "select t.id, t.name, o.name as deptName from ieasy_sys_deptinfo_category t join ieasy_sys_org o on(o.id=t.deptid)" ;
		List<DeptInfoCategoryForm> list = this.basedaoDeptInfoCategory.listSQL(sql, DeptInfoCategoryForm.class, false) ;
		return list;
	}

	@Override
	public List<DeptInfoCategoryForm> getCategoryByDept(String deptid) {
		String sql = "select t.id, t.name from ieasy_sys_deptinfo_category t where t.deptid=?" ;
		List<DeptInfoCategoryForm> list = this.basedaoDeptInfoCategory.listSQL(sql, new Object[]{deptid}, DeptInfoCategoryForm.class, false) ;
		return list;
	}

	
}
